package com.tbit.uqbike.webmanager.controller;

import com.github.pagehelper.PageInfo;
import com.tbit.uqbike.object.pojo.MachineCleanLog;
import com.tbit.common.entity.pojo.vo.DateResult;
import com.tbit.uqbike.object.pojo.statistics.ExportMachineCleanLog;
import com.tbit.uqbike.object.pojo.statistics.ExportPowerLog;
import com.tbit.uqbike.object.value.Page;
import com.tbit.uqbike.object.view.JsonResult;
import com.tbit.uqbike.service.business.MachineCleanLogService;
import com.tbit.uqbike.webmanager.util.Assert;
import com.tbit.uqbike.webmanager.util.PageHelperUtil;
import com.tbit.uqbike.webmanager.util.ServletUtils;
import com.tbit.uqbike.webmanager.util.excel.ExportExcelUtil;
import com.tbit.uqbike.webmanager.util.excel.ExportExcelWrapper;
import com.tbit.utils.DateTimeUtil;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.servlet.support.RequestContext;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletResponse;
import java.util.List;

/**
 * 清洁车日志(MachineCleanLog)表控制层
 *
 * @author jing
 * @since 2021-01-11 13:48:32
 */
@RestController
@RequestMapping("/machinecleanlog")
public class MachineCleanLogController extends BaseController {
    /**
     * 服务对象
     */
    @Resource
    private MachineCleanLogService machineCleanLogService;

    /**
     * 通过主键查询单条数据
     *
     * @param machineCleanLogId 主键
     * @return 单条数据
     */
    @PostMapping("/selectList")
    public JsonResult selectList(Integer machineCleanLogId) {
        return JsonResult.ok(this.machineCleanLogService.queryById(machineCleanLogId));
    }


    /**
     * 统计每天清洁车总数
     */
    @PostMapping("/statisticsByTime")
    public JsonResult statisticsByTime(MachineCleanLog machineCleanLog) {
        Assert.notNull(machineCleanLog.getAccountId(), "ver.areaId.notNull");
        Assert.notNull(machineCleanLog.getStartTime(), "sugar.startRideDt.notNull");
        Assert.notNull(machineCleanLog.getEndTime(), "sugar.endRideDt.notNull");
        List<DateResult> list = machineCleanLogService.statisticsByTime(machineCleanLog);
        return JsonResult.ok(list);
    }

    /**
     * 统计详情
     */
    @PostMapping("/queryPage")
    public JsonResult queryPage(MachineCleanLog machineCleanLog) {
        Assert.notNull(machineCleanLog.getAccountId(), "ver.areaId.notNull");
        Assert.notNull(machineCleanLog.getStartTime(), "sugar.startRideDt.notNull");
        Assert.notNull(machineCleanLog.getEndTime(), "sugar.endRideDt.notNull");
        machineCleanLog.setPage(new Page(machineCleanLog.getPageNO(), machineCleanLog.getRowCount()));
        PageHelperUtil.initPage(machineCleanLog.getPage());
        List<MachineCleanLog> machineCleanLogs = machineCleanLogService.queryPage(machineCleanLog);
        PageInfo<MachineCleanLog> pageInfo = new PageInfo<>(machineCleanLogs);
        return JsonResult.ok(pageInfo.getList(), (int) pageInfo.getTotal());
    }

    @GetMapping("/exportByTime")
    public void exportByTime(MachineCleanLog machineCleanLog, HttpServletResponse response) {
        Assert.notNull(machineCleanLog.getAccountId(), "ver.areaId.notNull");
        Assert.notNull(machineCleanLog.getStartTime(), "sugar.startRideDt.notNull");
        Assert.notNull(machineCleanLog.getEndTime(), "sugar.endRideDt.notNull");
//        if(null==machineCleanLog.getAccountUserId()){
//            throw new BaseException(SystemCodeEnum.SELECT_OPERATION_AND_MAINTENANCE_PERSONNEL);
//        }
        RequestContext context = getLogin(ServletUtils.getRequest()).getContext();
        List<ExportMachineCleanLog> exports = machineCleanLogService.exportByTime(machineCleanLog);
        ExportExcelWrapper<ExportMachineCleanLog> export = new ExportExcelWrapper();
        String filename = context.getMessage("excel_head_clear") + DateTimeUtil.getNowTimeYMD();
        String[] columnNames = {context.getMessage("excel_head_ridecard_name"), context.getMessage("excel.col.userCode"), context.getMessage("excel_head_ridelog_startTime"), context.getMessage("excel_head_ridelog_finishTime"), context.getMessage("excel_head_is_valid")};
        export.exportExcel(filename, filename, columnNames, exports, response, ExportExcelUtil.EXCEL_FILE_2003);
    }

    /**
     * 统计每个运维人员总数
     */
    @PostMapping("/getByType")
    public JsonResult getById(MachineCleanLog machineCleanLog) {
        Assert.notNull(machineCleanLog.getAccountId(), "ver.areaId.notNull");
        Assert.notNull(machineCleanLog.getStartTime(), "sugar.startRideDt.notNull");
        Assert.notNull(machineCleanLog.getEndTime(), "sugar.endRideDt.notNull");
        machineCleanLog.setPage(new Page(machineCleanLog.getPageNO(), machineCleanLog.getRowCount()));
        PageHelperUtil.initPage(machineCleanLog.getPage());
        List<ExportPowerLog> list = machineCleanLogService.getById(machineCleanLog);
        PageInfo<ExportPowerLog> pageInfo = new PageInfo<>(list);
        return JsonResult.ok(pageInfo.getList(), (int) pageInfo.getTotal());
    }

}